#define ACT_ENC 1
#define ACT_DEC 2
#define FULL_NAME "Universal Molotilka"
#define SHORT_NAME "umc"
#define FULL_VERSION "1.0.1-tiny"

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "cipher.h"
#include "cipher_init.h"
#include "common.h"
#include "hash.h"
void print_version()
{
    printf("%s %s\n", FULL_NAME, FULL_VERSION);
}    
void print_license()
{
    printf(
"Copyright (c) 2011, 2012, Alexey Silaev ArchFurry@gmail.com\n\
All rights reserved.\n\
\n\
Redistribution and use in source and binary forms, with or without modification,\n\
are permitted provided that the following conditions are met:\n\
\n\
    * Redistributions of source code must retain the above copyright notice, this \n\
        list of conditions and the following disclaimer.\n\
    * Redistributions in binary form must reproduce the above copyright notice, \n\
        this list of conditions and the following disclaimer in the documentation\n\
        and/or other materials provided with the distribution.\n\
\n\
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" \n\
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n\
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n\
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n\
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \n\
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, \n\
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF \n\
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n\
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n\
OF THE POSSIBILITY OF SUCH DAMAGE.\n");
}
void print_full_help()
{
    printf("%s [-d] -i in.file -o out.file -p password\n", SHORT_NAME);
    printf("\t-d Decrypt file\n");
    printf("\t-i Input file\n");
    printf("\t-o Output file\n");
    printf("\t-p Password\n");
    printf("\t-h Show this help\n");
    printf("\t-v View License(BSD)\n");
    exit(0);
}
void print_help()
{
    printf("%s [-d] -i in.file -o out.file -p password\n", SHORT_NAME);
    exit(0);
}
int main(int argc, char *argv[])
{
    int i;
    char action=ACT_ENC, in_num=0, out_num=0, p_num=0;
    uint8_t *key;
    FILE *in;
    struct UMcipher_context umc;
    struct UMhash_context sh;
    
    for(i=1;i<argc;i++){
        if(strcmp(argv[i], "-d")==0)
            action=ACT_DEC;
        if(strcmp(argv[i], "-i")==0)
            in_num=i+1;
        if(strcmp(argv[i], "-o")==0)
            out_num=i+1;
        if(strcmp(argv[i], "-p")==0)
            p_num=i+1;
        if(strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0 ){
            print_version();
            print_full_help();
        }
        if(strcmp(argv[i], "-v")==0 || strcmp(argv[i], "--version")==0 ){
            print_version();
            print_license();
            exit(0);
        }
    }
    
    if (in_num == 0){
        printf("Error: No input file (use -i flag)!\n");
        print_help();
        exit(1);
    }
    if (out_num == 0){
        printf("Error: No output file (use -o flag)!\n");
        print_help();
        exit(1);
    }
    if (p_num == 0){
        printf("Error: No password (use -p flag)!\n");
        print_help();
        exit(1);
    }
    if (strlen(argv[p_num]) == 0){
        printf("Error: Password is too short!\n");
        print_help();
        exit(1);
    }
    if (strlen(argv[out_num]) == 0){
        printf("Error: Output file name is too short!\n");
        print_help();
        exit(1);
    }
    
    in = fopen(argv[in_num], "rb");
    
    if (in == NULL){
        perror(argv[in_num]);
        exit(1);
    }
    if(fsize(in)<4){
        printf("Input file is too small!\n");
        exit(1);
    }
    fclose(in);
    
    UMhash_init(&sh);
    sh.hash_len=6;
    sh.ent_num=128;
    UMhash_calc_uchar(argv[p_num], strlen(argv[p_num]), &sh);
    key=UMcipher_token(sh.hash);
    
    UMcipher_init(4096, sh.hash[4], sh.hash[5], key, &umc);
    
    if(action==ACT_ENC)
    {
        UMcipher_file_encipher(argv[in_num], argv[out_num], &umc);
    }
    else
    {
        UMcipher_file_decipher(argv[in_num], argv[out_num], &umc);
    }
    
    bzero(key, 65536);
    bzero(sh.hash, 24);
    bzero(argv[p_num], strlen(argv[p_num]));
    
    exit(0);
}

